package your.packag;
import DAO.MyData;
import DTO.*;


import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

import org.apache.http.ParseException;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.cookie.Cookie;

import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import android.widget.TextView;

public class HistoryActivity extends Activity  {
	List<Cookie> cookie;
	 ListView mlistView;
	 ProgressDialog dialog;
	 List<HistoryDTO> listHistoryDTO = new ArrayList<HistoryDTO>();
	 private void loadList()
	 {
		 mlistView = (ListView) findViewById(R.id.listView1);

  		
	 }
		private Handler handler = new Handler() {
	        @Override
	        public void handleMessage(Message msg) {
	        	dialog.dismiss();
	        	loadList();

	        }
		};
		Runnable  loadData = new Runnable() {
			
			  public void run() {
	HttpGet httpGet = new HttpGet("http://smartwebvina.com/smartlive/index.php?option=com_mymonney");

		try {
			HttpResponse response = LoginActivity.postClient.execute(httpGet);
	    HttpEntity httpEntity =response.getEntity();
	    String xml;
			xml = EntityUtils.toString(httpEntity);
			org.jsoup.nodes.Document doc = Jsoup.parse(xml);
		      //org.jsoup.nodes.Document doc = Jsoup.connect("http://smartwebvina.com/smartlive/").get();
		      

		      
		        MyData data = new MyData();
		        
		        List<HistoryDTO> list = new ArrayList<HistoryDTO>();
		        listHistoryDTO = data.readHistory(doc);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		handler.sendEmptyMessage(0);
			  }};


	//TableLayout tl;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
        mlistView.setAdapter(new HistoryItemAdapter(this,
  				android.R.layout.simple_list_item_1, listHistoryDTO));
  		
  		mlistView.setOnItemClickListener(new OnItemClickListener() {

  			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  					long arg3) {
  				// TODO Auto-generated method stub
  				HistoryDTO temp = (HistoryDTO) mlistView.getItemAtPosition(arg2);
  				Intent intent = new Intent(HistoryActivity.this,DetailHistoryActivity.class);
  				intent.putExtra("Id", temp.Id());
  				intent.putExtra("Name", temp.Name());
  				intent.putExtra("Date", temp.Date());
  				intent.putExtra("Account", temp.Account());
  				intent.putExtra("Category", temp.Cateory());
  				intent.putExtra("Description", temp.Description());
  				intent.putExtra("Money", temp.Money());
  				
  				

  				startActivity(intent);

  				
  			}
  		});
        dialog = new ProgressDialog(this);

		//dialog.dismiss();
       
       

	}
	
	class HistoryItemAdapter extends ArrayAdapter<HistoryDTO> {
		private List<HistoryDTO> listhistoryDTO;

		HistoryItemAdapter(Context context, int textViewResourceId,
				List<HistoryDTO> listhistoryDTO) {
			super(context, textViewResourceId, listhistoryDTO);
			this.listhistoryDTO = listhistoryDTO;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.listitem_history, null);
			}

			HistoryDTO historyDTO = listhistoryDTO.get(position);
			if (listhistoryDTO != null) {
				TextView id = (TextView) view.findViewById(R.id.listitemid);
				TextView name = (TextView) view.findViewById(R.id.listitemname);
	
				
					
					id.setText(historyDTO.Id());
					name.setText(historyDTO.Name());

					
				

			}

			return view;

		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void onResume()
	{
		super.onResume();
        dialog = ProgressDialog.show(this, "", 
                "Loading. Please wait...", true);
        new Thread(loadData).start();
	}


	
}
